// pages/tenant/detail/detail.js
const app = getApp();
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tenant_id: "",
    tenant: {},
    house: {},
    show_power: false,
    power_btn_status: true,
    this_mouth_electric: "",
    this_mouth_water: "",
    active_list: [],
    charge_up_dialog_status: false,
    mode:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      tenant_id: options.id,
      mode:app.globalData.mode
    })
    this.getTenantDetail().then(house_id => {
      this.getHouseDetail(house_id)
    });
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarColor({
      backgroundColor: '#8fd3f4',
      frontColor: '#ffffff',
    })
    
  },
  // 获取住户基本信息
  async getTenantDetail() {
    return new Promise(resolve => {
      wx.request({
        url: 'https://api.liandunkaoqin.com/api/guestapi/dispatcher',
        method: "POST",
        header: {
          "Authorization": app.globalData.token
        },
        data: {
          "action": "include", //控制参数
          "type":"guest_id",
          "guest_id": this.data.tenant_id
        },
        success: res => {
          if (res.statusCode == 401) {
            app.login().then(res => {
              app.globalData.token = res;
              this.getTenantDetail().then(house_id => {
                this.getHouseDetail(house_id)
              });
            });
          } else {
            if (res.data.ret == 0) {
              this.setData({
                tenant: res.data.data[0]
              })
              let {
                guest_about_house_id
              } = res.data.data[0];
              resolve(guest_about_house_id);
            }
          }
        }
      })
    })

  },
  // 获取住户房间相关信息
  getHouseDetail(id) {
    wx.request({
      url: 'https://api.liandunkaoqin.com/api/houseapi/dispatcher',
      method: "POST",
      header: {
        "Authorization": app.globalData.token
      },
      data: {
        "action": "list_id", //控制参数
        "house_id": id
      },
      success: res => {
        if (res.statusCode == 401) {
          app.login().then(res => {
            app.globalData.token = res;
            this.getHouseDetail(id);
          });
        } else {
          if (res.data.ret == 0) {
            this.setData({
              house: res.data.data[0],
              show_power: res.data.data.length ? true : false
            })
          }
        }
      }
    })
  },
  // 点击按钮展示本月水电表数据
  showthisMouthPowerData() {
    let {
      house_id,
      house_electric,
      house_water
    } = this.data.house;
    wx.showLoading({
      title: '加载中...',
    })
    // 先查电表
    this.getThisMouthPowerData(house_id, house_electric).then(res => {
      if (res.code == 0) {
        this.setData({
          this_mouth_electric: (res.data.extendDict.deviceTotalData/1000).toFixed(2)
        });
        // 再查水表
        this.getThisMouthPowerData(house_id, house_water).then(res => {
          if (res.code == 0) {
            this.setData({
              power_btn_status: false,
              this_mouth_water: (res.data.extendDict.deviceTotalData/1000).toFixed(2)
            })
          } else {
            Dialog.alert({
              title: '提示',
              message: res.msg
            });
          }
        })
      } else {
        Dialog.alert({
          title: '提示',
          message: res.msg
        });
      }
    }).catch(res => {
      app.login().then(res => {
        app.globalData.token = res;
        Promise.all([this.getThisMouthPowerData(house_id, house_electric), this.getThisMouthPowerData(house_id, house_water)]).then(res => {
          // 先查电表
          if (res[0].code == 0) {
            this.setData({
              this_mouth_electric: (res.data.extendDict.deviceTotalData/1000).toFixed(2)
            });
            // 再查水表
            if (res[1].code == 0) {
              this.setData({
                power_btn_status: false,
                this_mouth_water: (res.data.extendDict.deviceTotalData/1000).toFixed(2)
              })
            } else {
              Dialog.alert({
                title: '提示',
                message: res.msg
              });
            }
          } else {
            Dialog.alert({
              title: '提示',
              message: res.msg
            });
          }
        })
      });
    }).finally(() => {
      wx.hideLoading();
    })
  },
  // 获取本月水电表数据
  async getThisMouthPowerData(house_id, device_id) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'https://api.liandunkaoqin.com/api/houseapi/dispatcher',
        method: "POST",
        header: {
          "Authorization": app.globalData.token
        },
        data: {
          "action": "add_tool", //控制参数
          "house_id": house_id,
          "tool_type": "value", //操作类型
          "tool_SN": device_id //水电表编码
        },
        success: res => {
          if (res.statusCode == 401) {
            reject(false);
          } else {
            resolve(res.data);
          }
        }
      })
    })
  },
  // 记账按钮
  chargeUp() {
    if(this.data.tenant.guest_price_type && this.data.tenant.guest_electric_price_type && this.data.tenant.guest_water_price_type){
      Dialog.alert({
        title:'提示',
        message:'无未收款项'
      })
    }else{
      this.setData({
        charge_up_dialog_status: true
      })
    }
  },
})